package com.test.daily.leetcode.y2022.m05.day0521.v03;

import java.util.Stack;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/21 下午 04:43
 * @version: 1.0
 */
public class Solution {
    public static class Node {
        int value;
        Node left;
        Node right;
    }

    public void pre(Node root) {
        if (root == null) {
            return;
        }
        if (root != null) {
            Stack<Node> stack = new Stack<>();
            stack.push(root);
            while (!stack.isEmpty()) {
                Node node = stack.pop();
                System.out.println(node.value);
                if (node.right != null) {
                    stack.push(node.right);
                }
                if (node.left != null) {
                    stack.push(node.left);
                }
            }
            System.out.println();
        }

    }

    public static void in(Node cur) {
        if (cur != null) {
            Stack<Node> stack = new Stack();
            while (!stack.isEmpty() || cur != null) {
                if (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    cur = stack.pop();
                    System.out.println(cur.value);
                    cur = cur.right;
                }
            }
        }
    }
    public static void pos1(Node head){
        System.out.println("post-order");
        if(head != null){
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            s1.add(head);
            while(!s1.isEmpty()){
                Node cur = s1.pop();
                s2.add(cur);
                if(cur.left != null){
                    s1.add(cur.left);
                }
                if(cur.right != null){
                    s1.add(cur.right);
                }
            }
            while(!s2.isEmpty()){
                System.out.println(s2.pop().value + " ");
            }
        }
    }
    public static void pos2(Node head){
        System.out.println("Post-Order:");
        if(head != null){
            Stack<Node> stack = new Stack<>();
            stack.add(head);
            while(!stack.isEmpty()){
                Node cur = stack.peek();
                if(cur.left!=null && head != cur.left && head !=cur.right){
                    stack.push(cur.left);
                } else if(cur.right != null && head != cur.right){
                    stack.push(cur.right);
                } else {
                    System.out.println(" "+cur.value);
                    head = cur;
                }
            }
        }
    }
}
